package com.myproject.music.controller;

import com.myproject.music.BizException;
import com.myproject.music.domain.User;
import com.myproject.music.service.UserService;
import com.myproject.music.util.AjaxResult;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiImplicitParam;
import io.swagger.annotations.ApiOperation;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;

import java.util.List;

@RestController
@RequestMapping("/user")
@Api(tags = "歌手管理0515郭彦良")
public class UserController {

    private static final Logger logger = LoggerFactory.getLogger(UserController.class);

    @Autowired
    private UserService userService;

    @ApiOperation("查询歌手列表")
    @ApiImplicitParam(name="name", value="歌手名字", dataType="string", dataTypeClass=String.class)
    @GetMapping("/list")
    public AjaxResult list(@RequestParam(value = "name", required = false) String userName) {
        logger.info("查询歌手列表，歌手名字：{}", userName);
        List<User> list = userService.selectUserByParam(userName);
        return AjaxResult.success(list);
    }

    @ApiOperation(value="获取歌手详细信息", notes = "根据歌手ID获取详细信息")
    @ApiImplicitParam(name = "id", value="歌手ID", required=true, dataType = "long", paramType="path", dataTypeClass=Long.class)
    @GetMapping(value = "/{id}")
    public AjaxResult getInfo(@PathVariable("id") Long id) {
        logger.info("获取歌手详细信息，歌手ID：{}", id);
        User user1 = userService.selectUserById(id);
        if(user1 == null || user1.getId() == null){
            logger.error("歌手{}不存在", id);
            throw new BizException(404,"歌手"+id+"不存在");
        }
        return AjaxResult.success(user1);
    }

    @ApiOperation("新增歌手")
    @PostMapping("/insertuser")
    public AjaxResult add(@Validated @RequestBody User user) {
        logger.info("新增歌手，歌手信息：{}", user);
        return AjaxResult.success(userService.insertUser(user));
    }

    @ApiOperation("修改歌手")
    @PutMapping
    public AjaxResult edit(@RequestBody User user) {
        logger.info("修改歌手，歌手信息：{}", user);
        return AjaxResult.success(userService.updateUser(user));
    }

    @ApiOperation("删除歌手")
    @ApiImplicitParam(name="id", value = "歌手ID", required = true, dataType = "long", paramType = "path", dataTypeClass = Long.class)
    @DeleteMapping("/{id}")
    public AjaxResult remove(@PathVariable Long id) {
        logger.info("删除歌手，歌手ID：{}", id);
        return AjaxResult.success(userService.deleteUserById(id));
    }

    @ApiOperation("查询全部歌手列表")
    @GetMapping("/userlist")
    public AjaxResult getAllUserList() {
        logger.info("查询全部歌手列表");
        return AjaxResult.success(userService.selectUserAll());
    }
}